package com.maple.soa.proxy;

import com.maple.soa.service.UserService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理-->代理所有接口类
 *
 * @author lijian
 * @date 2020/7/6
 */
public class ProxyHandler implements InvocationHandler {
    private Object object;
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public Object getProxy() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("当前用户：" + name);
        Object result = method.invoke(object, args);
        return result;
    }

}
